#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    long long distributeCandies(int n, int limit) {
        using LL = long long;
        LL res = 0;
        for (int i = 0; i <= limit; i++)
        {
            int remaining = n - i;
            int left = max(0, remaining - limit), right = min(limit, remaining);
            res += max(right - left + 1, 0);
        }
        return res;
    }
};